#include "WidgetVariantVec3.h"
#include "EMatrix4x4.h"
#include "EVariant.h"
#include "ui_WidgetVariantVec3.h"

WidgetVariantVec3::WidgetVariantVec3(QWidget *parent) :
    WidgetVariant(parent),
    ui(new Ui::WidgetVariantVec3)
{
    ui->setupUi(this);
}

WidgetVariantVec3::~WidgetVariantVec3()
{
    delete ui;
}

void WidgetVariantVec3::refreshByProperty(EItemMetaPropertyPtr p)
{
    WidgetVariant::refreshByProperty(p);
    this->clearConnect();

    EString text = p->nameCN.isEmpty() ? p->name : p->nameCN;
    ui->labelName->setText( text.data());

    p->getPropertyValue(p->value);

    ui->doubleSpinBox->setValue(p->value.to_EVec3().x);
    ui->doubleSpinBox_2->setValue(p->value.to_EVec3().y);
    ui->doubleSpinBox_3->setValue(p->value.to_EVec3().z);

    if (p->ifReadOnly == true)
    {
        ui->doubleSpinBox->setEnabled(false);
        ui->doubleSpinBox_2->setEnabled(false);
        ui->doubleSpinBox_3->setEnabled(false);
    }
    else
    {
        ui->doubleSpinBox->setEnabled(true);
        ui->doubleSpinBox_2->setEnabled(true);
        ui->doubleSpinBox_3->setEnabled(true);
    }

    this->buildConnect();
}

void WidgetVariantVec3::buildConnect()
{
	QObject::connect(
            ui->doubleSpinBox, 
            SIGNAL(valueChanged(double)), 
            this, 
            SLOT(slotValueChanged(double))
            );

	QObject::connect(
            ui->doubleSpinBox_2, 
            SIGNAL(valueChanged(double)), 
            this, 
            SLOT(slotValueChanged(double))
            );

	QObject::connect(
            ui->doubleSpinBox_3, 
            SIGNAL(valueChanged(double)), 
            this, 
            SLOT(slotValueChanged(double))
            );
}

void WidgetVariantVec3::clearConnect()
{
	QObject::disconnect(
            ui->doubleSpinBox, 
            SIGNAL(valueChanged(double)), 
            this, 
            SLOT(slotValueChanged(double))
            );

	QObject::disconnect(
            ui->doubleSpinBox_2, 
            SIGNAL(valueChanged(double)), 
            this, 
            SLOT(slotValueChanged(double))
            );

	QObject::disconnect(
            ui->doubleSpinBox_3, 
            SIGNAL(valueChanged(double)), 
            this, 
            SLOT(slotValueChanged(double))
            );
}

void WidgetVariantVec3::slotValueChanged(double value)
{
    EVec3 vec(
            ui->doubleSpinBox->value(),
            ui->doubleSpinBox_2->value(),
            ui->doubleSpinBox_3->value()
            );

    EVariant varOld = this->m_property->value;
    EVariant var(vec);
    this->m_property->setPropertyValue(var);
    emit signalVariantChanged(this->m_property, varOld);
}
